<?php


namespace App\Http\Controllers\Customer;


use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\Enterprises\StoreRequest;
use App\Http\Requests\Customer\Enterprises\UpdateRequest;
use App\Http\Searchs\Customer\EnterprisesSearch;
use App\Repositories\CustomerEnterprises;

class EnterprisesController extends Controller
{
    /**
     * @var CustomerEnterprises
     * @author luffyzhao@vip.126.com
     */
    private $enterprises;

    /**
     * EnterprisesController constructor.
     * @param CustomerEnterprises $enterprises
     * @author luffyzhao@vip.126.com
     */
    public function __construct(CustomerEnterprises $enterprises)
    {
        $this->enterprises = $enterprises;
    }

    /**
     * @param EnterprisesSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(EnterprisesSearch $search)
    {
        return $this->response(
            $this->enterprises->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            $this->enterprises->create($request->only([
                'name', 'customer_id', 'ebc_code', 'ebc_name', 'ebp_code', 'ebp_name', 'assure'
            ]))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->response(
            $this->enterprises->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            $this->enterprises->update($id, $request->only([
                'name', 'customer_id', 'ebc_code', 'ebc_name', 'ebp_code', 'ebp_name', 'assure'
            ]))
        );
    }
}